package com.eg366.nioLearn.baseSocket.chapter04;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

/**
 * 通过创建线程池进行调度
 */
public class TCPEchoServerExecutor {
	public static void main(String[] args) throws IOException {
		args = new String[] { "1122" };
		if (args.length != 1) {
			throw new IllegalArgumentException("Parameter(s):<Port>");
		}

		int echoServerPort = Integer.parseInt(args[0]);

		ServerSocket servSock = new ServerSocket(echoServerPort);

		Logger logger = Logger.getLogger("practical");

		/* 创建线程池 */
		Executor executor = Executors.newCachedThreadPool();
		while(true) {
			Socket socket = servSock.accept();
			executor.execute(new EchoProtocal(socket, logger));
		}
	}
}
